//
//  ForgetPsdViewController.m
//  hellomui
//
//  Created by zhangtao on 15/11/3.
//  Copyright © 2015年 zt.td. All rights reserved.
//

#import "ForgetPsdViewController.h"
#import "PsdCompViewController.h"
@interface ForgetPsdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNum;
@property (weak, nonatomic) IBOutlet UITextField *code;

@property (weak, nonatomic) IBOutlet UIButton *getYZM;
@property(nonatomic,strong)NSString *getCode;
@property(nonatomic,strong)NSString *codeTime;
@property (weak, nonatomic) IBOutlet UIButton *next;

@property(nonatomic,assign)NSUInteger count;   //计时
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *codeView;

@end

@implementation ForgetPsdViewController
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)getYZM:(id)sender {
    if (_count>0 && _count<59) {
        //
    }else{
    
    NSString *path = @"login/getPwdCode";
//    NSMutableDictionary *dics = [NSMutableDictionary dictionaryWithObjectsAndKeys:_phoneNum.text,@"mobile", nil];
    
    NSDictionary *dicss = @{@"mobile":_phoneNum.text};
    
    NSDictionary *dicVal = @{@"mobile":_phoneNum.text,@"bd_sig":[ToolObjc md5Str:dicss]};
    
    [[HttpConnctionManager sharedInstance]requestParam:dicVal andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        if ([[NSString stringWithFormat:@"%@",[succeedResult valueForKey:@"resCode"]] isEqualToString:@"0"]) {
            UIALERT(nil, @"验证码已发送至您的手机", nil, nil);

//            _getCode = [succeedResult stringWithFilted:@"code"];
            _codeTime = [succeedResult stringWithFilted:@"codeTime"];
        }else{
            UIALERT(nil, @"获取验证码失败", nil, nil);
        }
             [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
        
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        //
    }];
    }
}

- (void)countDown:(NSTimer *)timer
{
    [self.getYZM setTitle:[NSString stringWithFormat:@"%ld秒", (long)self.count] forState:UIControlStateNormal];
    self.count--;
    if(self.count <= 0)
    {
        
        [self.getYZM setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.getYZM setTitle:@" 重新获取" forState:UIControlStateNormal];
        [timer invalidate];
        self.count = 59;
    }
    
}


- (IBAction)next:(id)sender {
    if (_code.text.length != 0&& _codeTime ) {
//
        NSString *path = @"login/validCode";
        NSString *codeTime = [NSString stringWithFormat:@"%@",_codeTime];
        NSDictionary *dic = @{@"mobile":_phoneNum.text,@"code":_code.text,@"codeTime":codeTime};
        NSDictionary *dicVal =  @{@"mobile":_phoneNum.text,@"code":_code.text,@"codeTime":codeTime,@"bd_sig":[ToolObjc md5Str:dic]};
//
//        
        [[HttpConnctionManager sharedInstance]requestParam:dicVal andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
            if ([[NSString stringWithFormat:@"%@",[succeedResult valueForKey:@"resCode"]] isEqualToString:@"0"]) {
    
                PsdCompViewController *psd = [self.storyboard instantiateViewControllerWithIdentifier:@"psd"];
                psd.phoneNum = _phoneNum.text;
                
                [self presentViewController:psd animated:YES completion:nil];
            }else{
                [ToolObjc showMessage:@"验证错误"];
            }
            
            
        } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
            [ToolObjc showMessage:@"系统错误"];

        }];

           }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"验证码错误，请重新填写" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];

    }
//
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _count = 59;
    _phoneNum.placeholder = @"请输入手机号";
    _code.placeholder = @"请输入验证码";
    _phoneView.layer.cornerRadius = //_phoneView.frame.size.width/60;
    
    _codeView.layer.cornerRadius =// _phoneView.frame.size.width/60;
    _next.layer.cornerRadius = //_phoneView.frame.size.width/60;
    _getYZM.layer.cornerRadius = 9;//_getYZM.frame.size.width/40;
    // Do any additional setup after loading the view.
    
    _phoneView.layer.borderColor = rgb_box;
    _codeView.layer.borderColor = [RGB(131, 180, 240, 1)CGColor];

    
   _codeView.layer.borderWidth = _phoneView.layer.borderWidth = 1;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
